<?php
session_start();
include_once("../../untils/conn.php");
mysqli_query($con, "set names utf8");

$proxy_acc = $_POST['account'];
$proxy_pass = $_POST['password'];
$proxy_upid = $_POST['puid'];
$proxy_name = $_POST['username'];
$cr_time = time();

// 检查用户名是否为中文
// if (preg_match('/[\x{4e00}-\x{9fa5}]+/u', $proxy_name)) {
//   $response = array("code" => -4, "msg" => "用户名不能为中文");
//   echo json_encode($response);
//   exit();
// }

// 查询proxy表里是否存在相同的用户名
$checkUsernameQuery = "SELECT * FROM proxy WHERE proxy_acc = '$proxy_acc'";
$checkUsernameResult = mysqli_query($con, $checkUsernameQuery);

if (mysqli_num_rows($checkUsernameResult) > 0) {
  // 用户名已存在
  $response = array("code" => -1, "msg" => "用户名已存在");
  echo json_encode($response);
} else {
  // 验证密码是否符合要求（包含字母和数字，不低于10位）
  if (!preg_match('/^(?=.*[A-Za-z])(?=.*\d)[A-Za-z\d]{8,}$/', $proxy_pass)) {
    // 密码过于简单
    $response = array("code" => -2, "msg" => "密码过于简单，请设置包含不低于8位的字母和数字");
    echo json_encode($response);
  } else {
    // 验证邀请码是否存在
    $checkInvitationCodeQuery = "SELECT * FROM proxy WHERE proxy_id = '$proxy_upid'";
    $checkInvitationCodeResult = mysqli_query($con, $checkInvitationCodeQuery);

    if (mysqli_num_rows($checkInvitationCodeResult) == 0) {
      // 邀请码不存在
      $response = array("code" => 3, "msg" => "邀请码不存在");
      echo json_encode($response);
    } else {
      $group_id = "";
      $group_name = "";

      // 查询代理组信息
      $getGroupQuery = "SELECT * FROM proxy_group WHERE group_id IN (
        SELECT group_id FROM proxy WHERE proxy_id = '$proxy_upid'
      )";
      $getGroupResult = mysqli_query($con, $getGroupQuery);

      if (mysqli_num_rows($getGroupResult) > 0) {
        $groupData = mysqli_fetch_assoc($getGroupResult);
        $group_id = $groupData['group_id'];
        $group_name = $groupData['group_name'];
      }

      // 根据代理组信息查询新的group_id
      $newgroup_id = "";
      if ($group_name == "运营平台") {
        $getNewGroupQuery = "SELECT group_id FROM proxy_group WHERE group_name = '一级代理'";
        $getNewGroupResult = mysqli_query($con, $getNewGroupQuery);

        if (mysqli_num_rows($getNewGroupResult) > 0) {
          $newGroupData = mysqli_fetch_assoc($getNewGroupResult);
          $newgroup_id = $newGroupData['group_id'];
        }
      } elseif ($group_name == "一级代理") {
        $getNewGroupQuery = "SELECT group_id FROM proxy_group WHERE group_name = '二级代理'";
        $getNewGroupResult = mysqli_query($con, $getNewGroupQuery);

        if (mysqli_num_rows($getNewGroupResult) > 0) {
          $newGroupData = mysqli_fetch_assoc($getNewGroupResult);
          $newgroup_id = $newGroupData['group_id'];
        }
      } elseif ($group_name == "二级代理") {
        $getNewGroupQuery = "SELECT group_id FROM proxy_group WHERE group_name = '三级代理'";
        $getNewGroupResult = mysqli_query($con, $getNewGroupQuery);

        if (mysqli_num_rows($getNewGroupResult) > 0) {
          $newGroupData = mysqli_fetch_assoc($getNewGroupResult);
          $newgroup_id = $newGroupData['group_id'];
        }
      } elseif ($group_name == "三级代理") {
        // 该邀请码无法发展下级
        $response = array("code" => 4, "msg" => "该邀请码无法发展下级");
        echo json_encode($response);
        exit();
      }

      // 插入新数据到proxy表
      $insertQuery = "INSERT INTO proxy (proxy_acc, proxy_pass, proxy_upid, cr_time, group_id, proxy_name) VALUES ('$proxy_acc', '$proxy_pass', '$proxy_upid', '$cr_time', '$newgroup_id', '$proxy_name')";
      $insertResult = mysqli_query($con, $insertQuery);

      if ($insertResult) {
        $proxy_id = mysqli_insert_id($con); // 获取刚刚插入数据的自增ID

        // 将proxy_id插入到shopsy表中的puid字段中
        $sql = "INSERT INTO shopsy (puid, shop_name) VALUES ('$proxy_id', '$proxy_name')";
        $insertShopsyResult = mysqli_query($con, $sql);

        if ($insertShopsyResult) {
          // 注册成功
          $response = array("code" => 2, "msg" => "注册成功！请返回登录！");
          echo json_encode($response);
        } else {
          // 系统错误，请稍后再试
          $response = array("code" => -3, "msg" => "系统错误，请稍后再试！");
          echo json_encode($response);
        }
      } else {
        // 系统错误，请稍后再试
        $response = array("code" => -3, "msg" => "系统错误，请稍后再试！");
        echo json_encode($response);
      }
    }
  }
}

?>
